package com.ruoyi.shebei.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shebei.mapper.UpkeepMapper;
import com.ruoyi.shebei.domain.Upkeep;
import com.ruoyi.shebei.service.IUpkeepService;

/**
 * 保养记录Service业务层处理
 * 
 * @author admin
 * @date 2024-07-29
 */
@Service
public class UpkeepServiceImpl implements IUpkeepService 
{
    @Autowired
    private UpkeepMapper upkeepMapper;

    /**
     * 查询保养记录
     * 
     * @param upkeepId 保养记录主键
     * @return 保养记录
     */
    @Override
    public Upkeep selectUpkeepByUpkeepId(Long upkeepId)
    {
        return upkeepMapper.selectUpkeepByUpkeepId(upkeepId);
    }

    /**
     * 查询保养记录列表
     * 
     * @param upkeep 保养记录
     * @return 保养记录
     */
    @Override
    public List<Upkeep> selectUpkeepList(Upkeep upkeep)
    {
        List<Upkeep> upkeepList = upkeepMapper.selectUpkeepList(upkeep);
        List<Upkeep> resultList = new ArrayList<>();

        for (Upkeep item : upkeepList) {

            // 计算保养信息
            Date byDate = item.getByDate();
            LocalDate lastMaintenanceDate = byDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            long maintenancePeriodMonths = item.getByCycle();
            LocalDate nextByDate = lastMaintenanceDate.plusMonths(maintenancePeriodMonths);
            LocalDate currentDate = LocalDate.now();
            long daysUntilNextMaintenance = ChronoUnit.DAYS.between(currentDate, nextByDate);

            // 设置保养信息
            item.setNextByDate(nextByDate);
            item.setDaysUntilNextMaintenance(daysUntilNextMaintenance);

//            if (daysUntilNextMaintenance < 30) {
//                // ANSI转义码开始（红色）
//                final String ANSI_RED = "\u001B[31m";
//                // ANSI转义码结束
//                final String ANSI_RESET = "\u001B[0m";
//                System.out.println(ANSI_RED + "提醒：您的设备即将需要保养！" + ANSI_RESET);
//            } else {
//                System.out.println("您的设备目前不需要保养。");
//            }
        // 添加到结果列表
            resultList.add(item);
        }
        return resultList;
    }

    /**
     * 新增保养记录
     * 
     * @param upkeep 保养记录
     * @return 结果
     */
    @Override
    public int insertUpkeep(Upkeep upkeep)
    {
       upkeep.setCreateTime(DateUtils.getNowDate());
        return upkeepMapper.insertUpkeep(upkeep);
    }

    /**
     * 修改保养记录
     * 
     * @param upkeep 保养记录
     * @return 结果
     */
    @Override
    public int updateUpkeep(Upkeep upkeep)
    {
        return upkeepMapper.updateUpkeep(upkeep);
    }

    /**
     * 批量删除保养记录
     * 
     * @param upkeepIds 需要删除的保养记录主键
     * @return 结果
     */
    @Override
    public int deleteUpkeepByUpkeepIds(Long[] upkeepIds)
    {
        return upkeepMapper.deleteUpkeepByUpkeepIds(upkeepIds);
    }

    /**
     * 删除保养记录信息
     * 
     * @param upkeepId 保养记录主键
     * @return 结果
     */
    @Override
    public int deleteUpkeepByUpkeepId(Long upkeepId)
    {
        return upkeepMapper.deleteUpkeepByUpkeepId(upkeepId);
    }


}


